package com.platform.server.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.paltform.server.entity.ExcelCompany;
import com.paltform.server.entity.SysUser;
import com.platform.server.dao.WithdrawDao;
import com.platform.server.service.CompanyService;
import com.platform.server.util.ExportExcel;
import com.platform.server.util.StringTools;

@Controller
public class CompanyController {
	
	@Autowired
	private CompanyService companyService;
	
	@Autowired
	private WithdrawDao withdrawDao;
	
	/**
	 * 查看企业审核列表
	 * @return
	 */
	@RequestMapping("/selectCompanyList")
	public Object selectCompanyList(HttpServletRequest request,String userid,String audit,String company_name,String startTime,String endTime,String mobile,String address){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if("请选择分类".equals(audit)){
			audit = null;
		}
		List<Map<String, Object>> lists = companyService.selectCompanyList(userid, audit, company_name, startTime, endTime,mobile,address,null);
		for(int i = 0;i < lists.size();i++){
			lists.get(i).put("create_time",sdf.format(lists.get(i).get("create_time")));
		}
		request.setAttribute("lists", lists);
		request.setAttribute("nums", lists.size());
		if(!StringTools.isEmpty(userid)){
			return "/ui/company/company_info.jsp";
		}
		return "/ui/company/company_list.jsp";
	}
	
	/**
	 * 修改企业审核状态
	 */
	@RequestMapping("/updateCompanyStatus")
	public Object updateCompanyStatus(HttpSession session,String id,String aaudit,String username){
		//增加操作日志记录
		SysUser su = (SysUser) session.getAttribute("user");
		int flag = companyService.updateCompanyStatus(Integer.parseInt(id), aaudit,su.getSys_name());
		String user = null,content = null;
		if(su != null){
			user = su.getSys_name();
		}
		if("1".equals(aaudit)){
			List<Map<String, Object>> lists = companyService.selectCompanyList(null, null, null, null,null,null,null,id);
			Map<String,Object> map  = new HashMap<String,Object>();
			map.put("parent_id", lists.get(0).get("user_id"));
			List<Map<String,Object>> selectStatus = companyService.selectStatus(map);
			if(selectStatus!=null&&selectStatus.size()>0&&selectStatus.get(0).get("status")!=null&&selectStatus.get(0).get("status").toString().equals("1")){
				map.put("statu", "0");
				companyService.updateUserGrade(map);
			}
			content = "通过了" + username + "的企业审核";
		}else if("2".equals(aaudit)){
			content = "没有通过" + username + "的企业审核";
		}
		withdrawDao.insertLog(user, content);
		return "/selectCompanyList";
	}
	
	/**
	 * 修改企业审核状态
	 * @throws IOException 
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/batchCompanyStatus")
	public void batchCompanyStatus(HttpSession session,HttpServletResponse response,String aaudit,String id) throws Exception{
		String[] split = id.split(",");
		SysUser su = (SysUser) session.getAttribute("user");
		int flag=0;
		for (int i = 0; i < split.length; i++) {
			List<Map<String, Object>> lists = companyService.selectCompanyList(null, null, null, null,null,null,null,split[i]);
			if(!lists.get(0).get("audit").toString().equals("1")){
				flag=companyService.updateCompanyStatus(Integer.parseInt(split[i]), aaudit,su.getSys_name());
				String user = null,content = null;
				if(su != null){
					user = su.getSys_name();
				}
				if("1".equals(aaudit)){
					Map<String,Object> map  = new HashMap<String,Object>();
					map.put("parent_id", lists.get(0).get("user_id"));
					List<Map<String,Object>> selectStatus = companyService.selectStatus(map);
					if(selectStatus!=null&&selectStatus.size()>0&&selectStatus.get(0).get("status")!=null&&selectStatus.get(0).get("status").toString().equals("1")){
						map.put("statu", "0");
						companyService.updateUserGrade(map);
					}
					content = "通过了" + lists.get(0).get("username").toString() + "的企业审核";
				}else if("2".equals(aaudit)){
					content = "没有通过" + lists.get(0).get("username").toString() + "的企业审核";
				}
				withdrawDao.insertLog(user, content);
			}
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
        response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
	// 导出报表
	@RequestMapping("/companyExportExcel")
	public void companyExportExcel(HttpServletResponse response,String userid,String audit,String company_name,String startTime,String endTime,String mobile,String address) {
		List<Object> list2 = new ArrayList<Object>();
		List<Map<String, Object>> lists = companyService.selectCompanyList(userid, audit, company_name, startTime, endTime,mobile,address,null);
		for (Map<String, Object> map2 : lists) {
			ExcelCompany excelCompany = new ExcelCompany();
			if(map2.get("audit").toString().equals("0")){
				excelCompany.setAudit("待审核");
			}else if(map2.get("audit").toString().equals("1")){
				excelCompany.setAudit("审核通过");
			}else{
				excelCompany.setAudit("审核不通过");
			}
			
			excelCompany.setCompany_name(map2.get("company_name"));
			excelCompany.setCompany_person(map2.get("company_person"));
			excelCompany.setCreate_time(map2.get("create_time"));
			if(map2.get("identity_audit")!=null&&map2.get("identity_audit").toString().equals("1")){
				excelCompany.setIdentity_audit("已认证");
			}else if(map2.get("identity_audit")!=null&&map2.get("identity_audit").toString().equals("2")){
				excelCompany.setIdentity_audit("未认证");
			}else{
				excelCompany.setIdentity_audit("未认证");
			}
			
			excelCompany.setMobile(map2.get("mobile"));
			excelCompany.setPhone(map2.get("phone"));
			excelCompany.setUsername(map2.get("username"));
		list2.add(excelCompany);
		}
		String[] titles = { "姓名", "手机号", "身份认证", "企业昵称", "企业法人", "联系电话", "提交时间","审核状态" };
		ExportExcel.exportExcel("企业审核列表.xls", titles, list2, response);
	}
}
